<?php 
    global $curUser; 
    function checkData($username, $email){
        $check = array();
        $db = Database::getInstance();
        $userid = $_SESSION['userid'];
        $query = "SELECT * FROM users WHERE email = '$email' AND id != '$userid'";
        $result = $db->query($query);
        if($result->dataTable->numOfRow > 0)
            $check["email"] = "Email";
        $query = "SELECT * FROM users WHERE username = '$username' AND id != '$userid'";
        $result = $db->query($query);
        if($result->dataTable->numOfRow > 0)
            $check["username"] = "Username";
        return $check;
    }
    
    if (isset($_POST['user']) && sizeof(checkData($_POST['user']['login'], $_POST['user']['email'])) == 0) {
        $user = $_POST['user'];
        User::updateUser($user);
        if ($_SESSION['userid'])
            $curUser = User::withId($_SESSION['userid']);
        echo '<script>parent.window.location.reload(true);</script>';
    }else {
        $check = checkData($_POST['user']['login'], $_POST['user']['email']);
        $_POST["update"] = "";
        if($check["username"] != "")
            $_POST["update"] = $check["username"]." existed.";
        if($check["email"] != "")
            $_POST["update"] = $_POST["update"].$check["email"]." existed.";
    }
?>
<script>
    function validate(){
        var submit = true;
        document.getElementById('error_exist').style.display = "none";
        var user_name = document.forms[1].user_name;
        if(user_name.value === ""){
            user_name.previousSibling.style.color = 'red';
            submit = false;
        }else
            user_name.previousSibling.style.color = 'black';
        var user_login = document.forms[1].user_login;
        if(user_login.value === ""){
            user_login.previousSibling.style.color = 'red';
            submit = false;
        }else{
            var patt = new RegExp("^[a-z0-9_.-]{3,15}$");
            if(patt.test(user_login.value))
                switch(user_login.value.toUpperCase()){
                    case 'SHOTS':
                    case 'POPULAR':
                    case 'EVERYONE':
                    case 'DEBUTS':
                    case 'EXPLORE':
                    case 'HIGHLIGHTS':
                    case 'COLORS':
                    case 'ABOUT':
                    case 'SIGNUP':
                    case 'LOGIN':
                        user_login.previousSibling.style.color = 'red';
                        submit = false;
                        break;
                    default:
                        user_login.previousSibling.style.color = 'black';
                        document.getElementById('error').style.display = 'none';
                        break;
                }
            else{
                document.getElementById('error').style.display = 'inherit';
                submit = false;
            }
        }
        var user_email = document.forms[1].user_email;
        if(user_email.value === ""){
            user_email.previousSibling.style.color = 'red';
            submit = false;
        }else
            user_email.previousSibling.style.color = 'black';
        var user_address = document.forms[1].user_address;
        if(user_address.value === ""){
            user_address.previousSibling.style.color = 'red';
            submit = false;
        }else
            user_address.previousSibling.style.color = 'black';
        if(submit){
            document.forms[1].submit();
        }
    }
    $(function() {
        $("#user_birthday").datepicker({dateFormat: 'yy-mm-dd', yearRange : '1900:', changeMonth:true, changeYear: true});
    });
</script>
<div id="main">
  <ul class="tabs">
  <li class="active"><a href="<?php echo $this->baseUrl; ?>Account">Profile</a></li>
  <li><a href="<?php echo $this->baseUrl; ?>Account/Password">Password</a></li>
  <li><a href="<?php echo $this->baseUrl; ?>Account/Notifications">Notifications</a></li>
</ul>
    
<div class="session-form alt">
<form onsubmit="return false;" accept-charset="UTF-8" action="<?php echo $this->baseUrl; ?>Account" class="account gen-form with-messages" id="edit_user_456944" method="post"><div style="margin:0;padding:0;display:inline">
        <input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden" value="9c26/CY9n7HghzVay4MpBkrMdCyIgSl6eW/7gk9mc/g="></div>      
        <div class="form-field">
          <fieldset class="user_name"><label for="user_name">Name</label><input id="user_name" name="user[name]" size="30" type="text" value="<?php echo $curUser->name ?>"></fieldset>
          <p class="message">We're big on real names around here, so people know who's who</p>
        </div>
        
<div class="form-field"> 
    <fieldset class="user_login"><label for="user_login">Username</label><input autocapitalize="off" autocorrect="off" id="user_login" name="user[login]" size="30" type="text" value="<?php echo $curUser->username; ?>"></fieldset>
    <p class="message">
      Your ySocial URL: http://social.ythink.com.vn/<strong><span id="username"><?php echo $curUser->username; ?></span></strong>
    </p>
</div>
<div class="form-field">
  <fieldset class="user_email"><label for="user_email">Email</label><input id="user_email" name="user[email]" size="30" type="text" value="<?php echo $curUser->email; ?>"></fieldset>      
</div>

<div class="form-field">
  <fieldset class="user_address"><label for="user_address">Address</label><input id="user_address" name="user[address]" size="30" type="text" value="<?php echo $curUser->address; ?>"></fieldset>
</div>
        
<div class="form-field">
  <fieldset class="user_birthday"><label for="user_birthday">Birthday</label><input id="user_birthday" name="user[birthday]" size="30" type="text" value="<?php echo $curUser->birthday; ?>" readonly></fieldset>
  <p class="message">Format: yyyy-mm-dd.</p>
</div>
<div id="error" style="color: red; font-family: Calibri; font-size: 11pt;padding-bottom: 5px;display: none;margin-left: 135px;" align="center">
    Username may only contain A-Z, 0-9, underscores, dashes and periods
</div>
<div id="error_exist" style="width: 100%; margin-right: 143px; color: red; font-family: Calibri; font-size: 11pt;padding-bottom: 5px;<?php if(isset($_POST['user']) && $_POST['update'] != "") echo "display: inherit;"; else echo "display: none;"; ?>" align="center">
    <?php echo $_POST["update"]; ?>
</div>
<div class="form-btns">
  <input class="form-sub" name="update" type="submit" value="Update Settings" onclick="validate();">
  <a href="<?php echo $this->baseUrl; ?>Account/DestroyConfirm" class="link">Delete Your Account</a>
</div>
</form></div>
</div>